package cn.gson.financial.kernel.service.impl;

import cn.gson.financial.kernel.exception.ServiceException;
import cn.gson.financial.kernel.model.entity.User;
import cn.gson.financial.kernel.model.entity.UserAccountSets;
import cn.gson.financial.kernel.model.mapper.UserMapper;
import cn.gson.financial.kernel.model.vo.UserVo;
import cn.gson.financial.kernel.service.AccountSetsService;
import cn.gson.financial.kernel.service.CheckoutService;
import cn.gson.financial.kernel.service.UserAccountSetsService;
import cn.gson.financial.kernel.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author GaoYiJie
 * @Description
 * @Create 2025/4/28 10:33
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private UserAccountSetsService userAccountSetsService;
    private AccountSetsService accountSetsService;
    private CheckoutService checkoutService;

    @Override
    public int batchInsert(List<User> list) {
        return baseMapper.batchInsert(list);
    }

    /**
     * 手机号密码登录
     * @param mobile
     * @param password
     * @return
     */
    @Override
    public UserVo login(@NonNull String mobile, @NonNull String password) {
        //lambda方式的条件构造器
        LambdaQueryWrapper<User> query = Wrappers.lambdaQuery();
        query.eq(User::getMobile, mobile);//手机号
        //将前端传入的密码进行加密后进行eq比较
        query.eq(User::getPassword, DigestUtils.sha256Hex(password));
        //查询数据库有无用户信息
        User user = this.getOne(query);
        //没有
        if (user == null) {
            throw new ServiceException("账号或密码错误！");
        }
        //获取角色信息
        return getUserVo(user);
    }

    @Override
    public UserVo getUserVo(Integer userId) {
        User user = this.getById(userId);
        return getUserVo(user);

    }

    @Override
    public List<UserVo> listByAccountSetId(Integer accountSetsId) {
        return baseMapper.selectByAccountSetId(accountSetsId);
    }

    /**
     *
     * @param user
     * @return
     */
    private UserVo getUserVo(User user) {
        //创建用户关联账套表的wrapper
        LambdaQueryWrapper<UserAccountSets> qw = Wrappers.lambdaQuery();
        qw.eq(UserAccountSets::getAccountSetsId, user.getAccountSetsId());//用户的账套ID相等
        qw.eq(UserAccountSets::getUserId, user.getId());//用户ID相等
        //根据账套ID和用户ID查询UserAccountSets
        UserAccountSets userAccountSets = userAccountSetsService.getOne(qw);

        //返回结果的VO
        UserVo userVo = new UserVo();
        //如果用户有账套,返回账套信息
        if (user.getAccountSetsId() != null) {
            userVo.setAccountSets(accountSetsService.getById(user.getAccountSetsId()));
        }

        //如果用户关联账套,返回角色信息
        if (userAccountSets != null) {
            userVo.setRole(userAccountSets.getRoleType());
        }

        QueryWrapper cqw = Wrappers.query();
        cqw.eq("account_sets_id", user.getAccountSetsId());
        userVo.setCheckoutList(checkoutService.list(cqw));
        userVo.setAccountSetsList(accountSetsService.myAccountSets(user.getId()));
        userVo.setId(user.getId());
        userVo.setEmail(user.getEmail());
        userVo.setMobile(user.getMobile());
        userVo.setUnionId(user.getUnionId());
        userVo.setOpenId(user.getOpenId());
        userVo.setNickname(user.getNickname());
        userVo.setAvatarUrl(user.getAvatarUrl());
        userVo.setRealName(user.getRealName());
        userVo.setAccountSetsId(user.getAccountSetsId());
        userVo.setCreateDate(user.getCreateDate());
        return userVo;
    }
}


